package org.jeecg.modules.qyxx.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.qyxx.config.Comment;
import org.jeecg.modules.qyxx.entity.*;
import org.jeecg.modules.qyxx.entity.req.BgNsrjbxxReq;
import org.jeecg.modules.qyxx.enumdemo.RedisKeyConstant;
import org.jeecg.modules.qyxx.mapper.BgHzNdCwbbMapper;
import org.jeecg.modules.qyxx.mapper.BgSwzbfxCkbMapper;
import org.jeecg.modules.qyxx.service.IBgHzNdCwbbService;
import org.jeecg.modules.qyxx.service.IBgSwzbfxCkbService;
import org.jeecg.modules.qyxx.service.ICjQyxxStatusService;
import org.jeecg.modules.qyxx.util.DateRange;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.jeecg.modules.qyxx.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description: 财务指标分析汇总数据
 * @Author: ZJJ
 * @Date: 2021-06-10
 * @Version: V1.0
 */
// @DS("multi-datasource1")申报
@Service
public class BgHzNdCwbbServiceImpl extends MppServiceImpl<BgHzNdCwbbMapper, BgHzNdCwbb> implements IBgHzNdCwbbService {
    @Autowired
    private BgHzNdCwbbMapper bgHzNdCwbbMapper;
    @Autowired
    private IBgSwzbfxCkbService bgSwzbfxCkbService;
    @Autowired
    private ICjQyxxStatusService cjQyxxStatusService;

    @Override
    public Object hzNdCwbb(BgNsrjbxxReq req) {
        LambdaQueryWrapper<BgHzNdCwbb> wrapper = Wrappers.lambdaQuery(BgHzNdCwbb.class);
        String djxh = req.getDjxh();
        DateUtils utils = DateUtils.getInstance();
        List<String> years = utils.nearlyThreeYears(req.getKssj(), req.getJssj());
        wrapper.eq(BgHzNdCwbb::getDjxh, djxh).in(BgHzNdCwbb::getTjnd, years);
        List<BgHzNdCwbb> bgHzNdCwbbs = bgHzNdCwbbMapper.selectList(wrapper);
        Date now = req.getJssj();
        LambdaQueryWrapper<CjQyxxStatus> query = Wrappers.lambdaQuery(CjQyxxStatus.class);
        query.orderByDesc(CjQyxxStatus::getUpdateTime).eq(CjQyxxStatus::getType, "02").eq(CjQyxxStatus::getDjxh, djxh);
        //List<CjQyxxStatus> list = cjQyxxStatusService.list(query);
        /*if (list != null && list.size() > 0) {
            now = list.get(0).getUpdateTime();
        }*/
        for (BgHzNdCwbb bgHzNdCwbb : bgHzNdCwbbs) {
            if (bgHzNdCwbb.getYszk() != null) {
                bgHzNdCwbb.setYszk(bgHzNdCwbb.getYszk().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));

            }
            if (bgHzNdCwbb.getCh() != null) {
                bgHzNdCwbb.setCh(bgHzNdCwbb.getCh().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
            if (bgHzNdCwbb.getYylr() != null) {
                bgHzNdCwbb.setYylr(bgHzNdCwbb.getYylr().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
            if (bgHzNdCwbb.getJlr() != null) {
                bgHzNdCwbb.setJlr(bgHzNdCwbb.getJlr().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
            if (bgHzNdCwbb.getYysr() != null) {
                bgHzNdCwbb.setYysr(bgHzNdCwbb.getYysr().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
            if (bgHzNdCwbb.getJyhdcsdxjllje() != null) {
                bgHzNdCwbb.setJyhdcsdxjllje(bgHzNdCwbb.getJyhdcsdxjllje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
            String tjnd = bgHzNdCwbb.getTjnd();
            bgHzNdCwbb.setTsYear(tjnd + "年");
            if (utils.getYearToStr(now).equals(tjnd)) {
                if (utils.getYearToStr(now).equals(tjnd)) {
                    BigDecimal jdxh = bgHzNdCwbb.getJdxh();
                    if (jdxh != null) {
                        if (jdxh.equals(new BigDecimal(1))) {
                            bgHzNdCwbb.setTjnd(tjnd + "年(第一季度)");
                        } else if (jdxh.equals(new BigDecimal(2))) {
                            bgHzNdCwbb.setTjnd(tjnd + "年(第二季度)");
                        } else if (jdxh.equals(new BigDecimal(3))) {
                            bgHzNdCwbb.setTjnd(tjnd + "年(第三季度)");
                        }else {
                            bgHzNdCwbb.setTjnd(tjnd + "年");
                        }
                    } else {
                        bgHzNdCwbb.setTjnd(bgHzNdCwbb.getTjnd() + "年");
                    }
                }
               /* DateRange range = utils.getLastQuarter();
                Date end = range.getEnd();
                String dyjd = tjnd + "03";
                String dejd = tjnd + "06";
                String dsjd = tjnd + "09";
                // String dsjd = tjnd + "12";
                if (dyjd.equals(utils.getMonthToStr(end))) {
                    bgHzNdCwbb.setTjnd(tjnd + "年(第一季度)");
                } else if (dejd.equals(utils.getMonthToStr(end))) {
                    bgHzNdCwbb.setTjnd(tjnd + "年(第二季度)");
                } else if (dsjd.equals(utils.getMonthToStr(end))) {
                    bgHzNdCwbb.setTjnd(tjnd + "年(第三季度)");
                }*/
            } else {
                bgHzNdCwbb.setTjnd(bgHzNdCwbb.getTjnd() + "年");
            }
        }
        /*String tjnd = years.get(years.size() - 1);
         ConcurrentHashMap<String, Object> conMap = bgSwzbfxCkbService.getBgSwzbfxCkb();
        Map<String, Object> map = new HashMap<>();
        Field[] declaredFields = BgHzNdCwbb.class.getDeclaredFields();
        for (BgHzNdCwbb bgHzNdCwbb : bgHzNdCwbbs) {
            if (tjnd.equals(bgHzNdCwbb.getTjnd())) {
                for (Field declaredField : declaredFields) {
                    Comment annotation = declaredField.getAnnotation(Comment.class);
                    if (annotation != null) {
                        String name = declaredField.getName();
                        // BgHzNdCwbb ndCwbb = (BgHzNdCwbb)declaredField.get(bgHzNdCwbb);
                        BgSwzbfxCkb ckb = (BgSwzbfxCkb) conMap.get(name);
                        if (ckb != null) {
                            BigDecimal ckz = ckb.getCkz();
                            if ("chzzl".equals(name)) {
                                BigDecimal chzzl = bgHzNdCwbb.getChzzl();
                                if (chzzl != null) {
                                    Integer flag = chzzl.compareTo(ckz) <= 0 ? 1 : 2;
                                    map.put(name, flag);
                                } else {
                                    map.put(name, null);
                                }
                                continue;
                            }
                            if ("yszkzzl".equals(name)) {
                                BigDecimal yszkzzl = bgHzNdCwbb.getYszkzzl();
                                if (yszkzzl != null) {
                                    Integer flag = yszkzzl.compareTo(ckz) <= 0 ? 1 : 2;
                                    map.put(name, flag);
                                } else {
                                    map.put(name, null);
                                }
                                continue;
                            }
                            if ("qyjll".equals(name)) {
                                BigDecimal qyjll = bgHzNdCwbb.getQyjll();
                                if (qyjll != null) {
                                    Integer flag = qyjll.compareTo(ckz) <= 0 ? 1 : 2;
                                    map.put(name, flag);
                                } else {
                                    map.put(name, null);
                                }
                                continue;
                            }
                            if ("yyjll".equals(name)) {
                                BigDecimal yyjll = bgHzNdCwbb.getYyjll();
                                if (yyjll != null) {
                                    Integer flag = yyjll.compareTo(ckz) <= 0 ? 1 : 2;
                                    map.put(name, flag);
                                } else {
                                    map.put(name, null);
                                }
                                continue;
                            }
                            if ("yylrzzl".equals(name)) {
                                BigDecimal yylrzzl = bgHzNdCwbb.getYylrzzl();
                                if (yylrzzl != null) {
                                    Integer flag = yylrzzl.compareTo(ckz) <= 0 ? 1 : 2;
                                    map.put(name, flag);
                                } else {
                                    map.put(name, null);
                                }
                                continue;
                            }
                            if ("yysrzzl".equals(name)) {
                                BigDecimal yysrzzl = bgHzNdCwbb.getYysrzzl();
                                if (yysrzzl != null) {
                                    Integer flag = yysrzzl.compareTo(ckz) <= 0 ? 1 : 2;
                                    map.put(name, flag);
                                } else {
                                    map.put(name, null);
                                }
                                continue;
                            }
                            if ("yyxjbl".equals(name)) {
                                BigDecimal yyxjbl = bgHzNdCwbb.getYyxjbl();
                                if (yyxjbl != null) {
                                    Integer flag = yyxjbl.compareTo(ckz) <= 0 ? 1 : 2;
                                    map.put(name, flag);
                                } else {
                                    map.put(name, null);
                                }
                                continue;
                            }
                            if ("zcfzl".equals(name)) {
                                BigDecimal zcfzl = bgHzNdCwbb.getZcfzl();
                                if (zcfzl != null) {
                                    Integer flag = zcfzl.compareTo(ckz) < 0 ? 1 : 2;
                                    map.put(name, flag);
                                } else {
                                    map.put(name, null);
                                }
                                continue;
                            }
                            if ("sdbl".equals(name)) {
                                BigDecimal sdbl = bgHzNdCwbb.getSdbl();
                                if (sdbl != null) {
                                    Integer flag = sdbl.compareTo(ckz) <= 0 ? 1 : 2;
                                    map.put(name, flag);
                                } else {
                                    map.put(name, null);
                                }
                                continue;
                            }
                        }
                    }
                }
                break;
            }
        }
        map.put("bgHzNdCwbbs", bgHzNdCwbbs);*/
        return bgHzNdCwbbs;
    }

    @Override
    public BgHzNdCwbb getByDjxhAndTjnd(String djxh, String tjnd) {
        LambdaQueryWrapper<BgHzNdCwbb> wrapper = Wrappers.lambdaQuery(BgHzNdCwbb.class);
        wrapper.eq(BgHzNdCwbb::getDjxh, djxh).eq(BgHzNdCwbb::getTjnd, tjnd);
        return super.getOne(wrapper);
    }
}
